package com.jdz.gxq.info.admin.project.action;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.jdz.gxq.info.admin.project.bo.BuildingProject;
import com.jdz.gxq.info.admin.project.form.BuildingProjectForm;
import com.jdz.gxq.info.admin.project.service.BuildingProjectService;
import com.jdz.gxq.info.common.action.CommonDispatchAction;
import com.jdz.gxq.info.common.bo.PageBean;

public class BuildingProjectAction extends CommonDispatchAction {

	private BuildingProjectService buildingProjectService = null;
	 
	public ActionForward list(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws java.lang.Exception {
		
		BuildingProjectForm buildingProjectForm = (BuildingProjectForm) form;
		
		String projectName = buildingProjectForm.getProjectName();

		int totalCount = buildingProjectService.getBuildingProjectsCount(projectName);
		
		int currentPage = buildingProjectForm.getPageIndex();

		PageBean pageBean = new PageBean(totalCount, currentPage);
		
		buildingProjectForm.setPageIndex(pageBean.getCurrentPage());
		
		Map<String, Object> params = new HashMap<String, Object>();
		
		// 分页用
		params.put("startRow", pageBean.getPageStartRow() - 1);
		params.put("rowSize", pageBean.getPageSize());
		params.put("projectName", projectName);
		
		List<BuildingProject> buildingProjectList = buildingProjectService.getBuildingProjects(params);
		
		request.setAttribute("buildingProjectList", buildingProjectList);
		request.setAttribute("pageBean", pageBean);
		
		return mapping.findForward("success");
	}
	
	public ActionForward add(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws java.lang.Exception {
		
		BuildingProjectForm buildingProjectForm = (BuildingProjectForm) form;
		
		BuildingProject buildingProject = buildingProjectForm.getBuildingProject();

		buildingProject.setModifiedBy(getUserId(request));
		
		buildingProjectService.insertBuildingProject(buildingProject);

		return mapping.findForward("success");
	}
	
	public ActionForward gotoEdit(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws java.lang.Exception {
		
		BuildingProjectForm buildingProjectForm = (BuildingProjectForm) form;
	
		BuildingProject buildingProject = buildingProjectService.getBuildingProjectById(buildingProjectForm.getId());
		
		buildingProjectForm.setBuildingProject(buildingProject);

		return mapping.findForward("success");
	}
	
	public ActionForward save(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws java.lang.Exception {
		
		BuildingProjectForm buildingProjectForm = (BuildingProjectForm) form;
		
		BuildingProject buildingProject = buildingProjectForm.getBuildingProject();
		
		buildingProject.setModifiedBy(getUserId(request));
		
		buildingProjectService.updateBuildingProject(buildingProject);
		
		return mapping.findForward("success");
	}
	
	public ActionForward delete(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws java.lang.Exception {
		
		String projectIdsStr = request.getParameter("id");
		
		String[] projectIds = projectIdsStr.split(",");
		
		for (String projectId : projectIds) {
			
			BuildingProject buildingProject = new BuildingProject();
			buildingProject.setProjectId(Integer.parseInt(projectId));
			buildingProject.setProjectStatus("2");
			buildingProject.setHandDepart("");
			buildingProject.setHandLeader("");
			buildingProject.setModifiedBy(getUserId(request));
			
			buildingProjectService.deleteBuildingProject(buildingProject);
		}

		return mapping.findForward("success");
	}

	public BuildingProjectService getBuildingProjectService() {
		return buildingProjectService;
	}

	public void setBuildingProjectService(
			BuildingProjectService buildingProjectService) {
		this.buildingProjectService = buildingProjectService;
	}
}

